﻿using System;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class Plans : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("NA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                lblAirline.Text = Session["AIRLINE"].ToString();
                if (Session["FLT_NO"] != null)
                    txtFltNo.Text = Session["FLT_NO"].ToString();
            }
            lblError.Text = "";
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnSubmit.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnApprove.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnReject.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnPublish.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");

            if (lstPlanStatus.SelectedValue == "E")
            {
                MultiView1.ActiveViewIndex = 0;
                btnSubmit.Enabled = true;
                btnReject.Enabled = false;
                btnApprove.Enabled = false;
                btnPublish.Enabled = false;
                chkDepInit.Enabled = false;
                btnDelete.Enabled = true;
            }
            else if (lstPlanStatus.SelectedValue == "S")
            {
                MultiView1.ActiveViewIndex = -1;
                btnSubmit.Enabled = false;
                btnReject.Enabled = true;
                btnApprove.Enabled = true;
                btnPublish.Enabled = false;
                chkDepInit.Enabled = false;
                btnDelete.Enabled = false;
            }
            else if (lstPlanStatus.SelectedValue == "A")
            {
                MultiView1.ActiveViewIndex = -1;
                btnSubmit.Enabled = false;
                btnReject.Enabled = true;
                btnApprove.Enabled = false;
                btnPublish.Enabled = true;
                chkDepInit.Enabled = true;
                btnDelete.Enabled = false;
            }
            else//Published
            {
                MultiView1.ActiveViewIndex = -1;
                btnSubmit.Enabled = false;
                btnReject.Enabled = false;
                btnApprove.Enabled = false;
                btnPublish.Enabled = false;
                chkDepInit.Enabled = false;
                btnDelete.Enabled = false;
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            Session["FLT_NO"] = txtFltNo.Text;

            if (lstPlanStatus.SelectedValue == "E")
                txtFltNo2.Text = txtFltNo.Text;

            /*switch (lstPlanStatus.SelectedValue)
            {
                case "E":
                    MultiView1.ActiveViewIndex = 0;
                    btnSubmit.Visible = true;
                    btnApprove.Visible = false;
                    btnPublish.Visible = false;
                    btnReject.Visible = false;
                    btnDelete.Visible = true;
                    break;
                case "S":
                    MultiView1.ActiveViewIndex = -1;
                    btnSubmit.Visible = false;
                    btnApprove.Visible = true;
                    btnPublish.Visible = false;
                    btnReject.Visible = true;
                    btnDelete.Visible = false;
                    break;
                case "A":
                    MultiView1.ActiveViewIndex = -1;
                    btnSubmit.Visible = false;
                    btnApprove.Visible = false;
                    btnPublish.Visible = true;
                    btnReject.Visible = true;
                    btnDelete.Visible = false;
                    break;
                case "P":
                    MultiView1.ActiveViewIndex = -1;
                    btnSubmit.Visible = false;
                    btnApprove.Visible = false;
                    btnPublish.Visible = false;
                    btnReject.Visible = false;
                    btnDelete.Visible = false;
                    break;
                default:
                    lblError.Text = "Bad Plans Status!";
                    break;
            }*/


            //如果航班计划已过期180天，可以删除（存储过程中判断）
            //if (lstPlanStatus.SelectedValue == "P")
            //{
            //    btnDelete.Enabled = false;
            //}
            //else
            //{
            //    btnDelete.Enabled = true;
            //}
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            try
            {
                DateTime.Parse(txtDateFrom.Text);
                DateTime.Parse(txtDateTo.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }
            
            //if (lstTaxType.SelectedValue == "?")
            //{
            //    lblError.Text = "Please select aircraft size!";
            //    return;
            //}
            int ret_code;
            if (txtFltNo2.Text == "")
            {
                lblError.Text = "Please input flight number!";
                return;
            }
            if (txtFltNo2.Text.IndexOf(",") >= 0)
            {
                lblError.Text = "Flight number should not include comma( ,)!";
                return;
            }
            StringBuilder week_mask = new StringBuilder(8);
            if (chkWeek1.Checked)
                week_mask.Append("1");
            else
                week_mask.Append("_");
            if (chkWeek2.Checked)
                week_mask.Append("2");
            else
                week_mask.Append("_");
            if (chkWeek3.Checked)
                week_mask.Append("3");
            else
                week_mask.Append("_");
            if (chkWeek4.Checked)
                week_mask.Append("4");
            else
                week_mask.Append("_");
            if (chkWeek5.Checked)
                week_mask.Append("5");
            else
                week_mask.Append("_");
            if (chkWeek6.Checked)
                week_mask.Append("6");
            else
                week_mask.Append("_");
            if (chkWeek7.Checked)
                week_mask.Append("7");
            else
                week_mask.Append("_");
            try
            {
                SqlCommand cmd = new SqlCommand("bll_plan_new");
                SqlParameter para = cmd.Parameters.AddWithValue("@id", txtPlanId.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", txtFltNo2.Text);
                //para = cmd.Parameters.AddWithValue("@op_airline", Session["AIRLINE"].ToString());
                //para = cmd.Parameters.AddWithValue("@op_flt_no", txtFltNo2.Text);
                para = cmd.Parameters.AddWithValue("@week_mask", week_mask.ToString());
                para = cmd.Parameters.AddWithValue("@week_interval", lstWeekInterval.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@flt_type", lstFltType.SelectedValue);
                para = cmd.Parameters.AddWithValue("@date_from", txtDateFrom.Text);
                para = cmd.Parameters.AddWithValue("@date_to", txtDateTo.Text);
                para = cmd.Parameters.AddWithValue("@dep_time_nature", txtDepTime.Text);
                para = cmd.Parameters.AddWithValue("@initial_status", lstStatus.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@valid_date", txtValidDate.Text);
                para = cmd.Parameters.AddWithValue("@tail_no", lstTailNo.SelectedValue);
                para = cmd.Parameters.AddWithValue("@surcharge_type", lstSurchargeType.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    txtFltNo.Text = txtFltNo2.Text;
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Adding plan failed!";
                else if (ret_code == 2)
                    lblError.Text = "Invalid plan period!";
                else if (ret_code == 3)
                    lblError.Text = "Aircraft not found!";
                else if (ret_code == 4)
                    lblError.Text = "Flight number can not be 'OPEN'!";
                else if (ret_code == 5)
                    lblError.Text = "Copy seat map failed!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the plan you want to delete.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_delete";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_no", strArray[1]);
                para = cmd.Parameters.AddWithValue("@id", strArray[2]);

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else
                    lblError.Text = "delete table " + ret_code.ToString() + " failed!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the plan you want to submit.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SetPlanStatus(strArray[0], strArray[1], strArray[2], "S");
        }

        protected void SetPlanStatus(string airline, string flt_no, string id, string action)
        {
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_set_status";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", airline);
                para = cmd.Parameters.AddWithValue("@flt_no", flt_no);
                para = cmd.Parameters.AddWithValue("@id", id);
                para = cmd.Parameters.AddWithValue("@action", action);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1 || ret_code == 2 || ret_code == 3)
                {
                    lblError.Text = "Failed!";
                }
                else if (ret_code == 4)
                {
                    lblError.Text = "Bad Action!";
                }
                else if (ret_code == 5)
                {
                    lblError.Text = "Some virtual flight were not authorized!";
                }
                else if (ret_code == 10)
                {
                    lblError.Text = "Improper route data!!";
                }
                else if (ret_code == 11)
                {
                    lblError.Text = "No segment information!";
                }
                else if (ret_code == 12)
                {
                    lblError.Text = "No cabin information, or insufficient!";
                }
                else if (ret_code == 13)
                {
                    lblError.Text = "Please calculate nested availble first!";
                }
                else if (ret_code == 14)
                {
                    lblError.Text = "No class information, or insufficient!";
                }
                else if (ret_code == 15)
                {
                    lblError.Text = "Lost some stop!";
                }
                //else if (ret_code == 16)
                //{
                //    lblError.Text = "Duplicate airport exist!";
                //}
                else
                {
                    lblError.Text = "Error " + ret_code.ToString() + " !";
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnApprove_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the plan you want to approve.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SetPlanStatus(strArray[0], strArray[1], strArray[2], "A");
        }
        protected void btnReject_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the plan you want to reject.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SetPlanStatus(strArray[0], strArray[1], strArray[2], "R");
        }
        //protected void btnSegments_Click(object sender, EventArgs e)
        //{
        //    string key;
        //    string[] strArray;
        //    if (Request.Form["KEY"] == null)
        //    {
        //        lblError.Text = "Please select a plan.";
        //        return;
        //    }
        //    key = Request.Form["KEY"];
        //    strArray = key.Split('|');

        //    Response.Redirect("plan_segs.aspx?flt_no=" + strArray[1] + "&id=" + strArray[2]);
        //}
        
        protected void btnPublish_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the plan.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_publish";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", strArray[1]);
                para = cmd.Parameters.AddWithValue("@plan_id", strArray[2]);
                para = cmd.Parameters.AddWithValue("@with_dep_initiating", chkDepInit.Checked);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                    MultiView1.ActiveViewIndex = -1;
                }
                else if (ret_code == 1)
                    lblError.Text = "Plan not found!";
                else if (ret_code == 2)
                    lblError.Text = "Improper route data!";
                else if (ret_code == 3)
                    lblError.Text = "Flight segment data not found!";
                else if (ret_code == 4)
                    lblError.Text = "Improper Leg/Cabin data!";
                else if (ret_code == 5)
                    lblError.Text = "Improper Seg/Class data!";
                else if (ret_code == 6)
                    lblError.Text = "Please calculate nested avail. first!";
                else if (ret_code == 7)
                    lblError.Text = "Some virtual flight were not authorized!";
                else if (ret_code == 8)
                    lblError.Text = "Can not convert city time!";
                else if (ret_code == 10)
                    lblError.Text = "Adding flight failed!";
                else if (ret_code == 11)
                    lblError.Text = "Adding flight route failed!";
                else if (ret_code == 12)
                    lblError.Text = "Adding flight segment failed!";
                else if (ret_code == 13)
                    lblError.Text = "Adding flight cabins failed!";
                else if (ret_code == 14)
                    lblError.Text = "Adding flight calsses failed!";
                else if (ret_code == 15)
                    lblError.Text = "Stop number is not sequential!";
                else if (ret_code == 19)
                    lblError.Text = "Adding virtual flight failed!";
                else if (ret_code == 20)
                    lblError.Text = "Updating plan status failed!";
                else if (ret_code == 21)
                    lblError.Text = "Initiat seat map failed!";
                else
                    lblError.Text = "Error=" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                return;
            }
        }
        protected void btnLog_Click(object sender, EventArgs e)
        {
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select a plan.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');
            //Response.Redirect("plan_logs.aspx?flt_no=" + strArray[1] + "&id=" + strArray[2]);
            
            //由于Plan的日志存在通用日志中，所以暂不能按航班计划ID查！
            Response.Redirect("PlanLogs.aspx?flt_no=" + txtFltNo.Text);
        }
        protected void btnSeatCtl_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the plan.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("PlanInventory.aspx?flt_no=" + strArray[1] + "&id=" + strArray[2]);
        }

        protected void btnRoute_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the plan.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("PlanRoute.aspx?flt_no=" + strArray[1] + "&id=" + strArray[2]);
        }
        protected void btnSeatMap_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the plan.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("PlanSeatMap.aspx?flt_no=" + strArray[1] + "&id=" + strArray[2]);
        }
    }
}